<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../js/jquery-3.4.1.js"></script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<script type="text/javascript">

    class BusinessError extends Error {

    }

    $(function () {
        //init1();
        init2();
    })

    function init1() {
        getData1("./data1.json", {}, function (data) {
            const {id} = data;
            getData1("./data2.json", {id}, function (data) {
                const {username} = data;
                getData1("./data3.json", {username}, function (data) {
                    console.log("init1", data);
                });
            });
        });
    }

    function init2() {
        getData2("data1.json", {}).then((data) => {
            if (typeof data === "undefined" || data === null) {
                return;
            }
            const {id} = data;
            console.log("step1");
            //throw new BusinessError("error1");
            return getData2("data2.json", {id});
        }).catch((reason => {
            // 发生业务异常时的判断
            if (reason instanceof BusinessError) {
                console.log("business catch1", reason);
            }
            console.log("catch1", reason);
        })).then((data) => {
            // then链中如果上一步执行失败, 这一步接收到的内容是undefined
            if (typeof data === "undefined" || data === null) {
                return;
            }
            const {username} = data;
            console.log("step2");
            return getData2("data3.json", {username});
        }).catch((reason => {
            // 发生业务异常时的判断
            if (reason instanceof BusinessError) {
                console.log("business catch2", reason);
            }
            console.log("catch2");
        })).then((data) => {
            if (typeof data === "undefined" || data === null) {
                return;
            }
            console.log("step3");
            console.log("init2", data);
        }).catch((reason => {
            // 发生业务异常时的判断
            if (reason instanceof BusinessError) {
                console.log("business catch3", reason);
            }
            console.log("catch3");
        }));
    }

    function getData1(url, data = {}, successCallback, errorCallback) {
        $.ajax({
            type: "get",
            url: url,
            data: data,
            success: successCallback,
            error: errorCallback
        });
    }

    function getData2(url, data = {}) {
        return new Promise((resolve, reject) => {
            $.ajax({
                type: "get",
                url: url,
                data: data,
                success: function (data) {
                    if (data.code !== 10000) {
                        reject(data.errMsg);
                        return;
                    }
                    resolve(data);
                },
                error: function () {
                    reject();
                }
            });
        });
    }


</script>
</body>
</html>